package com.jia.interviewPro;

import com.sun.org.apache.xml.internal.utils.FastStringBuffer;

/**
 * @program: Leetcode
 * @description: 02.08.环路监测
 * @author: STU756
 * @create: 2020-08-05 01:30
 */
public class Pro0208 {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null) { return null; }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) { break; }
        }
        fast = head;
        while(fast != null && slow != null && slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}
